package com.dindang.checkline.mvp.ui.activity.adapter;

import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.dindang.checkline.R;
import com.dindang.checkline.mvp.greendao.entity.CheckItem;
import com.dindang.checkline.mvp.model.entity.CheckItemEntity;

import java.util.List;

public class ChooseItemAdapter extends RecyclerView.Adapter<ChooseItemAdapter.ViewHolder> {

    List<CheckItem> list;
    String[] states = {"待检查", "正常", "异常"};
    public ChooseItemAdapter(List<CheckItem> list) {
        this.list = list;
    }
    MyOnClickListener myOnClickListener;

    public void setMyOnClickListener(MyOnClickListener myOnClickListener) {
        this.myOnClickListener = myOnClickListener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.choose_item_item, parent, false);
        ChooseItemAdapter.ViewHolder viewHolder = new ChooseItemAdapter.ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        CheckItem flow = list.get(position);
        //检查内容
        holder.content_tv.setText(flow.getC_content());
        //状态
        switch (flow.getC_status()) {
            case "0":// 待检查
                holder.status_tv.setText(states[0]);
                holder.status_tv.setBackgroundResource(R.mipmap.daijiancha);
                holder.status_tv.setTextColor(Color.BLUE);
                break;
            case "1":// 正常
                holder.status_tv.setText(states[1]);
                holder.status_tv.setBackgroundResource(R.mipmap.zhengchang);
                holder.status_tv.setTextColor(Color.WHITE);
                break;
            case "8":// 异常
                holder.status_tv.setText(states[2]);
                holder.status_tv.setTextColor(Color.WHITE);
                holder.status_tv.setBackgroundResource(R.mipmap.yichang);
                break;
            default:
                holder.status_tv.setText(states[0]);
                holder.status_tv.setBackgroundResource(R.mipmap.daijiancha);
                holder.status_tv.setTextColor(Color.BLUE);
                break;
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(list.get(position).getC_status().equals("0")){//未巡检,点击开始巡检
                    myOnClickListener.onScanClick(position);
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

//    @Override
//    public void onClick(View v) {
//        if(list.get(position).getC_status().equals("0")){//未巡检,点击开始巡检
//            myOnClickListener.onScanClick(position);
//        }
//    }


    class ViewHolder extends RecyclerView.ViewHolder {
        TextView content_tv;
        TextView status_tv;
        ViewHolder(View itemView) {
            super(itemView);
            content_tv = itemView.findViewById(R.id.content);
            status_tv = itemView.findViewById(R.id.statue);
        }
    }
    public interface MyOnClickListener {
        void onScanClick(int position);
    }
}